package com.daxue.se.io;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author daxue0929
 * @date 2023/01/01
 **/

public class FileTest {

    public String filePath;

    @BeforeEach
    public void init() throws IOException {
        filePath = new File("..").getCanonicalPath() + File.separator + "files";
    }


    @Test
    public void test003() throws IOException {
        File file = new File(filePath, "test004.exe");
        if (!file.exists()) {
            System.out.println("file 是否创建成功: " + file.createNewFile());
        }
        File files = new File(filePath);
        if (files.isDirectory()) {
            File[] listFiles = files.listFiles((dir, name) -> name.endsWith("exe"));
            Arrays.stream(listFiles).forEach(item -> System.out.println(item.getAbsolutePath()));
        }
    }

    @Test
    public void test002() throws IOException {
        File test002Name = new File(filePath, "test002_name");
        if (!test002Name.exists()) {
            System.out.println("test002Name文件是否创建成功:" + test002Name.createNewFile());
        }

        System.out.println("test002Name文件是否是可执行的:" +  test002Name.canExecute());
        System.out.println("test002Name文件是否是可读的:" + test002Name.canRead());
        System.out.println("test002Name文件是否还可写的:" + test002Name.canWrite());

        File test002Name2 = new File(filePath, "test002_name");
        System.out.println(test002Name.compareTo(test002Name2));

        System.out.println(test002Name.getPath());
        System.out.println(test002Name.getTotalSpace());

        System.out.println("这个抽象路径是否是绝对的: " + test002Name.isAbsolute());
        System.out.println("这个抽象路径是否是目录: " + test002Name.isDirectory());
        System.out.println("这个抽象路径是否为普通文件: " + test002Name.isFile());
        System.out.println("这个抽象路径命名的文件是否是隐藏的: " + test002Name.isHidden());
        System.out.println("返回此抽象路径表示的文件上次修改的时间:" + test002Name.lastModified());
        System.out.println("返回此抽象路径名表示的文件的长度:" + test002Name.length());
        // 如果是文件就返回null
        System.out.println("返回一个数组,此抽象路径下目录下的文件列表:" + test002Name.listFiles());

        System.out.println("构造一个表示此抽象路径名的 file: URI :  " + test002Name.toURI());
        System.out.println(test002Name.toPath());
        File[] files = File.listRoots();
        System.out.println("列出可用的文件系统根: " + files.toString());

        File file = new File(filePath, "test003.txt");
        System.out.println("重命名test002Name是否成功: " + test002Name.renameTo(file));

        System.out.println("=========================================================");
        System.out.println("=========================================================");

        // windows下是在这个路径下C:\Users\Admin\AppData\Local\Temp
        File tempFile = File.createTempFile("daxue_temp", "txt");
        System.out.println(tempFile.getAbsolutePath());

        System.out.println(tempFile.delete());

    }

//    @Test
    public void  test001() throws IOException {
        String separator = File.separator;
        String pathSeparator = File.pathSeparator;
        char pathSeparatorChar = File.pathSeparatorChar;
        System.out.println("separator is: " + separator);
        System.out.println("pathSeparator is: " + pathSeparator);
        System.out.println("=========================================================");

        // 获取当前路径的第一种方法.通过File对象.
        File directory = new File("abc");
        System.out.println(directory.getCanonicalPath()); //得到的是D:\IdeaProjects\bootprojects\apache-common-demo\abc
        System.out.println(directory.getAbsolutePath());  //得到的是D:\IdeaProjects\bootprojects\apache-common-demo\abc
        System.out.println(directory.getPath());          //得到的是abc

        File directory2 = new File(".");
        System.out.println(directory2.getCanonicalPath()); //得到的是D:\IdeaProjects\bootprojects\apache-common-demo
        System.out.println(directory2.getAbsolutePath());  //得到的是D:\IdeaProjects\bootprojects\apache-common-demo\.
        System.out.println(directory2.getPath());          //得到的是.

        File directory3 = new File("..");
        System.out.println(directory3.getCanonicalPath()); //得到的是D:\IdeaProjects\bootprojects
        System.out.println(directory3.getAbsolutePath());  //得到的是D:\IdeaProjects\bootprojects\apache-common-demo\..
        System.out.println(directory3.getPath());
        System.out.println("=========================================================");

        File file = new File("..");
        System.out.println(file.getCanonicalPath());
    }
}
